package collection;


import banklogicimpl.AbstractAccount;

public class AccountsSet {
    private int setSize;
    private int numInSet;
    AbstractAccount[] accountsSet;
    AbstractAccount[] newAccountsSet;

    public AccountsSet(int setSize){
        this.setSize = setSize;
        accountsSet = new AbstractAccount[setSize];
    }

    public void put(AbstractAccount account){
        this.accountsSet[numInSet] = account;
        numInSet++;
        if (numInSet > 0.6*setSize) rebuild(setSize*2);
    }

    private void rebuild(int newSize){
        newAccountsSet = new AbstractAccount[newSize];
        for (int i = 0; i < numInSet; i++) newAccountsSet[i] = accountsSet[i];
        accountsSet = newAccountsSet;
        setSize = newSize;
    }

    public AbstractAccount get(){
        if (numInSet==0) return null;
        return accountsSet[numInSet-1];
    }

    public AbstractAccount get(int number){
        return accountsSet[number];
    }

    public void clean(){
        accountsSet = new AbstractAccount[setSize];
        numInSet = 0;
    }

    public int size(){
        return setSize;
    }

    public boolean isEmpty(){
        if (numInSet == 0) return false;
        return true;
    }

}
